package com.newedu.equipmgr.servlet.system.user;

import com.alibaba.fastjson.JSONObject;
import com.newedu.equipmgr.entity.SysUser;
import com.newedu.equipmgr.service.SysUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name="UserAddServlet",urlPatterns = {"/api/servlet/user/addwithrole"})
public class UserAddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);

        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        String roleId = req.getParameter("role");

        System.out.println(username);
        System.out.println(pwd);


        SysUserService areaService = new SysUserService();
        //boolean insert = areaService.insert(new SysUser(username, pwd));
        boolean insert = areaService.insertUserAndRoleRelationship(new SysUser(username, pwd),Integer.parseInt(roleId));
        JSONObject jsonObject = new JSONObject();
        // {"message": "添加成功", "status":"success"}
        if(insert){//成功 {"message": "添加成功", "status":"success"}
            jsonObject.put("message","添加成功");
            jsonObject.put("status","success");

        }else{// 失败 {"message": "添加失败", "status":"fail"}
            jsonObject.put("message","添加失败");
            jsonObject.put("status","fail");
        }

        resp.setContentType("application/json;charset=utf-8");
        String jsonString = jsonObject.toJSONString();
        System.out.println(jsonString);
        resp.getWriter().println(jsonString);
    }
}